package com.atguigu.eduservice.service.impl;

import com.atguigu.commonutils.result.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.mapper.EduTeacherMapper;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author kabuda
* @description 针对表【edu_teacher(讲师)】的数据库操作Service实现
* @createDate 2023-04-24 14:04:24
*/
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher>
    implements EduTeacherService{

    @Autowired
    private EduCourseService courseService;

    @Override
    public HashMap<String, Object> pageCondition(TeacherQuery teacherQuery) {
        Page<EduTeacher> page = new Page<>(teacherQuery.getCurrentPage(), teacherQuery.getPageSize());

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        Date begin = teacherQuery.getBegin();
        Date end = teacherQuery.getEnd();

        LambdaQueryWrapper<EduTeacher> wrapper = new LambdaQueryWrapper<>();

        wrapper
                .like(StringUtils.hasText(name), EduTeacher::getName,name)
                .eq(level!=null,EduTeacher::getLevel,level)
                .ge(begin!=null,EduTeacher::getGmtCreate,begin)
                .le(end!=null,EduTeacher::getGmtCreate,end)
                .orderByDesc(EduTeacher::getGmtCreate)
        ;

        IPage<EduTeacher> teacherIPage = this.baseMapper.selectPage(page, wrapper);
        List<EduTeacher> records = teacherIPage.getRecords();
        long total = teacherIPage.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("items",records);
        map.put("total",total);
        return map;
    }

    /**
     * id降序获取前四个
     * @return
     */
    @Cacheable(value = "banner",key = "'indexTeacher'")
    @Override
    public List<EduTeacher> indexTeacher() {
        LambdaQueryWrapper<EduTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(EduTeacher::getId);
        wrapper.last("limit 8");
        List<EduTeacher> list = this.list(wrapper);
        return list;
    }

    @Override
    public Map<String, Object> pageTeacher(Long page, Long limit) {
        Page<EduTeacher> pageParam = new Page<EduTeacher>(page,limit);

        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        baseMapper.selectPage(pageParam, queryWrapper);
        List<EduTeacher> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    @Override
    public R getTeacherInfo(String teacherId) {
        EduTeacher teacher = this.getById(teacherId);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id",teacherId);
        wrapper.orderByDesc("gmt_modified");
        List<EduCourse> courseList = courseService.list(wrapper);
        return R.ok().data("teacher", teacher).data("courseList", courseList);
    }
}




